% code for reading an rts file 18.07.2000
%latest date 13.05.2001


[file,path] = uigetfile('*.rts', 'Open raw time series');

 if file == 0,
 K = 2;
 return;
 end;

line1 = [path file];
fid = fopen(line1,'rb'); 

if (fid == -1)
	fprintf('\n\n\n\n    Error in opening %s\n',line1);
	fprintf('        ....file does not exists or path may not be correct\n');
 	return;
	end;
disp('reading time series');
[nchannel,Count]  = fread(fid,1,'ushort',2);
[MaxRecord,Count] = fread(fid,1,'unsigned long');
[Record2,Count] = fread(fid,1,'unsigned long');
[Record3,Count] = fread(fid,1,'unsigned long');
[Record4,Count] = fread(fid,1,'unsigned long');
[Record5,Count] = fread(fid,1,'unsigned long',24);

[Bdst,Count] = fread(fid,[16 4],'uchar');

[Exd, Count] = fread(fid,8,'uchar');
[Eyd, Count] = fread(fid,8,'uchar');
%[dummy,Count] = fread(fid,128,'uchar');
[dummy,Count] = fread(fid,56,'uchar');
[Filt,Count]  = fread(fid,[5 1],'ushort');
[dummy,Count] = fread(fid,62,'uchar');
[Exa, Count] = fread(fid,4,'uchar');
[Eya, Count] = fread(fid,4,'uchar');
[Hxa, Count] = fread(fid,4,'uchar');
[Hya, Count] = fread(fid,4,'uchar');
[Hza, Count] = fread(fid,4,'uchar');
[dummy,Count] = fread(fid,492,'uchar');
[Record1,Count] = fread(fid,1,'unsigned long');
[dummy,Count] = fread(fid,12,'uchar');
[Bdst(:,5),Count] = fread(fid,16,'uchar');
[Bden,Count] = fread(fid,[16 5],'uchar');
[Events,Count]  = fread(fid,[1 5],'ushort');
[hpf,Count]  = fread(fid,1,'ushort');
[NotchF,Count]  = fread(fid,[5 1],'float');
[dummy,Count] = fread(fid,112,'uchar');

if sum([Record1,Record2,Record3,Record4,Record5]) ~= max([Record1,Record2,Record3,Record4,Record5]),
fprintf('The file contains more than one band\n');
fprintf('Exits..');
fclose(fid);
return;
end;

Bddm(:,1) = Bdst(:,5);
Bddm(:,2) = Bdst(:,1);
Bddm(:,3) = Bdst(:,2);
Bddm(:,4) = Bdst(:,3);
Bddm(:,5) = Bdst(:,4);
Bdst = Bddm;


NBlocks = max([Record1,Record2,Record3,Record4,Record5]);
nband = find([Record1,Record2,Record3,Record4,Record5] > 0); 
nlines =( (NBlocks/nchannel) * 256 ) / 4;
array = [nlines nchannel];
[matrix,Count] = fread(fid,array,'long');
fclose(fid);

if Count~= nlines*nchannel,
fprintf('Error in source file reading\n');
return;
end;


Exd = str2num(setstr(Exd'));			%e - probe spacing
Eyd = str2num(setstr(Eyd'));
Exa = str2num(setstr(Exa'));			%channel amplification
Eya = str2num(setstr(Eya'));
Hxa = str2num(setstr(Hxa'));
Hya = str2num(setstr(Hya'));
Hza = str2num(setstr(Hza'));

Bdstart = setstr(Bdst(:,nband))';
Bdend   = setstr(Bden(:,nband))';

SBS = size(Bdstart);
SBE = size(Bdend);
	
a = str2num([Bdstart(7:8) Bdstart(1:2) Bdstart(4:5) Bdstart(9:10) Bdstart(12:13) Bdstart(15:16)]);

day = str2num(Bdstart(4:5));
hr = str2num(Bdstart(9:10));
mi = str2num(Bdstart(12:13));
se = str2num(Bdstart(15:16));
	
StID = day*24*3600 + hr*3600 + mi*60 + se;
